package com.example.t410.service.serviceImpl;

import com.example.t410.dao.LunTan;
import com.example.t410.dao.NootBook;
import com.example.t410.dao.PingLun;
import com.example.t410.service.LunTanService;
import com.mongodb.client.result.DeleteResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.data.mongodb.core.MongoTemplate;
import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class LunTanServiceImpl implements LunTanService {
    @Autowired
    MongoTemplate mongoTemplate;
//    查询根据类别
    @Override
    public List<LunTan> selectType(String type) {
            if(type.equals("jiuye")){
                type="学业就业";
            } else if (type.equals("lianai")) {
                type="恋爱关系";
            }
            else if (type.equals("guanxi")){
                type="人际关系";
            } else if (type.equals("chengzhang")) {
                type="个人成长";
            }
            Query query = new Query();
            Criteria criteria = Criteria.where("type").is(type);
            query.addCriteria(criteria);
            List<LunTan> lunTanList = mongoTemplate.find(query, LunTan.class);
            return lunTanList;

    }
//  发表论坛
    @Override
    public void insert(LunTan lunTan) {
        Random rd=new Random();
        lunTan.setComTime(System.currentTimeMillis());
        lunTan.setLikeNum(rd.nextInt(15));
        lunTan.setComNum(0);
        mongoTemplate.insert(lunTan);
    }
//  删除
    @Override
    public void delectById(String Id) {
        Query queryLunTan=new Query(Criteria.where("_id").is(Id));
        Query queryPingLun=new Query(Criteria.where("lunTanId").is(Id));
        DeleteResult resultLunTan=mongoTemplate.remove(queryLunTan,LunTan.class);
        DeleteResult resultPingLun=mongoTemplate.remove(queryPingLun, PingLun.class);
        long countLunTan = resultLunTan.getDeletedCount();
        long countPingLun=resultPingLun.getDeletedCount();
        log.info("成功删除:{} 条论坛",countLunTan);
        log.info("成功删除:{} 条评论",countPingLun);
    }
}
